// axios 模块
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'

// 导入nprogress 包对应的js和css
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 配置默认值
// 设置根路径
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/'
// 请求拦截器
axios.interceptors.request.use(request => {
  // 开启进度条
  NProgress.start()
  // 统一添加登录认证
  if (sessionStorage.getItem('token')) {
    // 路由导航，获取hash#之后的路径
    let pathname = location.hash.substr(1)
    if (pathname !== '/' && pathname !== '/login') {
      request.headers.common['Authorization'] = sessionStorage.getItem('token')
    }
  }
  return request
}, error => {
  return Promise.reject(error)
})
// 响应拦截器
axios.interceptors.response.use(response => {
  // 隐藏进度条
  NProgress.done()
  if (response.status === 200) {
    response = response.data
  }
  return response
}, error => {
  return Promise.reject(error)
})
Vue.use(VueAxios, axios)
